﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using SERIX.Classes;
using System.Text;
using System.Collections.Generic;
namespace SERIX
{
    public partial class Programacao : Seguranca
    {


        protected void Page_Load(object sender, EventArgs e)
        {
            base.OnLoad(e);

            if (!IsPostBack)
            {
                int anoLiberado = CPlanejamento.AnoProgramacaoCorrente(this.usuAut.Login);
                int ano = DateTime.Now.Year;
                for (int i = ano - 4; i <= ano + 2; i++)
                {
                    string valor = i.ToString();
                    ddlAno.Items.Add(new ListItem(valor, valor, true));
                }
                ddlAno.SelectedValue = anoLiberado.ToString();
            }

        }

        protected void ddlIndicadores_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void odsIndicadores_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            e.InputParameters["login"] = usuAut.Login;
            e.InputParameters["idt_prog"] = ddlPrograma.SelectedValue;
        }



        protected void ddlAcao_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void odsAcao_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            e.InputParameters["login"] = UsuAut.Login;
        }

        protected void cbxProgramadoHeader_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cbx = (CheckBox)sender;
            bool check = false;
            if (cbx.Checked)
                check = true;
            foreach (GridViewRow row in gridView.Rows)
            {
                ((CheckBox)row.FindControl("cbxProgramado")).Checked = check;
            }
        }

        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView row = (DataRowView)e.Row.DataItem;
                if (row["Idt_TPo_MEd"] != DBNull.Value)
                    ConfiguraMedida(e.Row.Cells[1], "1", row["Idt_TPo_MEd"].ToString(), row["valor_Prog"].ToString());
                else
                {
                    e.Row.Cells[1].Enabled = false;
                    //((RequiredFieldValidator)e.Row.Cells[1].Controls[1]).Enabled = false;
                }
                if (row["Idt_TPo_MEd2"] != DBNull.Value)
                    ConfiguraMedida(e.Row.Cells[2], "2", row["Idt_TPo_MEd2"].ToString(), row["valor_Prog2"].ToString());
                else
                {
                    e.Row.Cells[2].Enabled = false;
                    //((RequiredFieldValidator)e.Row.Cells[2].Controls[1]).Enabled = false;
                }
                if (row["Idt_TPo_MEd3"] != DBNull.Value)
                    ConfiguraMedida(e.Row.Cells[3], "3", row["Idt_TPo_MEd3"].ToString(), row["valor_Prog3"].ToString());
                else
                {
                    e.Row.Cells[3].Enabled = false;
                    //((RequiredFieldValidator)e.Row.Cells[3].Controls[1]).Enabled = false;
                }
            }
        }

        private void ConfiguraMedida(TableCell cell, string ordem, string Idt_Tpo_Med, string valor_Prog)
        {
            DataTable dte = CProgramacao.ObterMedida(Idt_Tpo_Med, usuAut.Login);
            if (dte.Rows.Count > 0)
            {
                ((Label)cell.FindControl("Medida" + ordem)).Text = dte.Rows[0]["dsc_tpo_Med"].ToString();
                ((TextBox)cell.FindControl("Valor" + ordem)).Text = valor_Prog;
            }
        }

        protected void btnConfirmar_Click(object sender, EventArgs e)
        {
            List<StructProgramacao> programas = new List<StructProgramacao>();
            foreach (GridViewRow row in gridView.Rows)
            {
                if (gridView.DataKeys[row.RowIndex]["Idt_Tpo_Med"] != null)
                {
                    StructProgramacao chave = new StructProgramacao();
                    chave.Idt_AIS = Convert.ToInt32(gridView.DataKeys[row.RowIndex]["idt_AIS"]);
                    chave.idt_Ind = Convert.ToInt32(gridView.DataKeys[row.RowIndex]["idt_ind"]);
                    TextBox tbx = (TextBox)row.Cells[1].FindControl("Valor1");//Primeira Ordem
                    if (!string.IsNullOrEmpty(tbx.Text))
                    {
                        chave.Idt_Tpo_Med = Convert.ToInt32(gridView.DataKeys[row.RowIndex]["Idt_Tpo_Med"]);
                        chave.valor = Convert.ToDecimal(tbx.Text);
                        tbx = (TextBox)row.Cells[2].FindControl("Valor2");//Segunda Ordem
                        if (!string.IsNullOrEmpty(tbx.Text))
                        {
                            chave.Idt_Tpo_Med2 = Convert.ToInt32(gridView.DataKeys[row.RowIndex]["Idt_Tpo_Med2"]);
                            chave.valor2 = Convert.ToDecimal(tbx.Text);
                            tbx = (TextBox)row.Cells[3].FindControl("Valor3");//Terceira Ordem
                            if (!string.IsNullOrEmpty(tbx.Text))
                            {
                                chave.Idt_Tpo_Med3 = Convert.ToInt32(gridView.DataKeys[row.RowIndex]["Idt_Tpo_Med3"]);
                                chave.valor3 = Convert.ToDecimal(tbx.Text);
                            }
                        }
                    }
                    programas.Add(chave);
                }
            }
            if (programas.Count > 0)
            {
                string idt_Tec = CTecnicos.PesquisaporCodUsu(usuAut.Codigo, usuAut.Login).Idt_tec;
                string idt_Mun = ddlMunicipios.SelectedValue;
                CProgramacao.GravaProgramacao(programas, idt_Tec, idt_Mun, usuAut.Login);
                lblmsg.Text = "Operação realizada com Sucesso";
                lblmsg.Visible = true;
            }
        }

        protected void odsMunicipios_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            e.InputParameters["idt_prog"] = ddlPrograma.SelectedValue;
            e.InputParameters["pms_Ano"] = ddlAno.SelectedValue;
            e.InputParameters["login"] = UsuAut.Login;
            lblmsg.Visible = false;
        }

        protected void ddlMunicipios_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlPrograma.DataBind();
            ddlAcao.DataBind();
            gridView.DataBind();
            lblmsg.Visible = false;
        }

        protected void odsMunicipios_Selecting1(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            e.InputParameters["login"] = this.usuAut.Login;
            e.InputParameters["idt_tec"] = CTecnicos.PesquisaporCodUsu(this.usuAut.Codigo, this.usuAut.Login).Idt_tec;
        }

        protected void odsIndicadores_Selected(object sender, ObjectDataSourceStatusEventArgs e)
        {
            //if (e.ReturnValue != null && ((DataTable)e.ReturnValue).Rows.Count > 0)
            //{
            //    btnConfirmar.Enabled = true;
            //}
            //else
            //    btnConfirmar.Enabled = false;
        }

        protected void odsIndicadores_Selecting1(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            e.InputParameters["idt_prog"] = ddlPrograma.SelectedValue;
            e.InputParameters["idt_tec"] = CTecnicos.PesquisaporCodUsu(usuAut.Codigo, usuAut.Login).Idt_tec;
            e.InputParameters["login"] = UsuAut.Login;
            lblmsg.Visible = false;
        }

        protected void ddlAno_SelectedIndexChanged(object sender, EventArgs e)
        {
            gridView.DataBind();
            int ANO_AIS = Convert.ToInt32(ddlAno.SelectedValue);
            if (!CPlanejamento.AnoLiberadoPlanejamento(ANO_AIS, UsuAut.Login))
            {
                gridView.Enabled = false;
                lblmsg.Visible = true;
                lblmsg.Text = "Modificação da Programação bloqueado para esse ano.";
            }
            else
            {
                gridView.Enabled = true;
                lblmsg.Visible = false;
            }
        }

        protected void ddlMunicipios_DataBinding(object sender, EventArgs e)
        {
            DropDownList ddl = (DropDownList)sender;
            ddl.Items.Clear();
            ddl.Items.Add(new ListItem(":.Selecione um item.:", string.Empty));
        }

        protected void odsPrograma_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            e.InputParameters["login"] = usuAut.Login;
        }

        protected void ddlPrograma_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlAcao.DataBind();
            gridView.DataBind();
        }
    }
}
